﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SPCFMedicineInformer.Helpers
{
    public class StringUtils
    {
        #region TruncateIfLonger (overloaded)

        /// <summary>
        /// Truncates a string if it exeeds a specified number of characters.
        /// </summary>
        /// <param name="input">The string to be truncated if needed.</param>
        /// <param name="maxLength">The maximum number of characters in the output string, including the optional postfix.</param>
        /// <param name="postfix">One or more characters (ie "...") to be appended to the truncated string, if truncation happens.</param>
        /// <param name="breakWord">True if truncation is allowed to break words. If false truncation will only break a word if no space is found within the maximum output string length.</param>
        /// <param name="truncated">Output parameter notifying if the string has been truncated.</param>
        /// <returns>The truncated string, or the original string if no truncation has occured.</returns>
        public static string TruncateIfLonger(string input, int maxLength, string postfix, bool breakWord, out bool truncated)
        {
            if (input.Length <= maxLength)
            {
                truncated = false;
                return input;
            }

            int internalMaxLength = (postfix == null) ? maxLength : maxLength - postfix.Length;
            if (internalMaxLength <= 0)
            {
                throw new ArgumentException();
            }

            int breakPos = breakWord ? internalMaxLength : input.LastIndexOf(' ', internalMaxLength);
            if (breakPos <= 0)
            {
                breakPos = internalMaxLength;
            }

            truncated = true;
            return string.Concat(input.Substring(0, breakPos), postfix,"...");
        }

        /// <summary>
        /// Truncates a string if it exeeds a specified number of characters.
        /// </summary>
        /// <param name="input">The string to be truncated if needed.</param>
        /// <param name="maxLength">The maximum number of characters in the output string, including the optional postfix.</param>
        /// <param name="postfix">One or more characters (ie "...") to be appended to the truncated string, if truncation happens.</param>
        /// <param name="breakWord">True if truncation is allowed to break words. If false truncation will only break a word if no space is found within the maximum output string length.</param>
        /// <returns>The truncated string, or the original string if no truncation has occured.</returns>
        public static string TruncateIfLonger(string input, int maxlength, string postfix, bool breakWord)
        {
            bool truncated;
            return TruncateIfLonger(input, maxlength, postfix, breakWord, out truncated);
        }

        /// <summary>
        /// Truncates a string if it exeeds a specified number of characters.
        /// Using this overload truncation will only break a word if no space is found within the maximum output string length.
        /// </summary>
        /// <param name="input">The string to be truncated if needed.</param>
        /// <param name="maxLength">The maximum number of characters in the output string, including the optional postfix.</param>
        /// <returns>The truncated string, or the original string if no truncation has occured.</returns>
        public static string TruncateIfLonger(string input, int maxlength)
        {
            return TruncateIfLonger(input, maxlength, null, false);
        }

        #endregion
        #region StringManipulator
        public static string RemoveLastSymbol(string str,string delimiter)
        {
            if (!string.IsNullOrEmpty(str))
            {
                return str.Length > 0 ? str.Remove(str.LastIndexOf(delimiter), delimiter.Length) : str;
            }
            return "";
        }
        public static string StripTagsCharArray(string source)
        {
            char[] array = new char[source.Length];
            int arrayIndex = 0;
            bool inside = false;

            for (int i = 0; i < source.Length; i++)
            {
                char let = source[i];
                if (let == '<')
                {
                    inside = true;
                    continue;
                }
                if (let == '>')
                {
                    inside = false;
                    continue;
                }
                if (!inside)
                {
                    array[arrayIndex] = let;
                    arrayIndex++;
                }
            }
            return new string(array, 0, arrayIndex);
            }

        #endregion
    }
}